#!/bin/bash

# rotationd is part of code.google.com/p/camtools, Copyright 2007 Camille Huot
# and is licensed under http://www.gnu.org/copyleft/gpl.html

# rotationd depends on app-laptop/tp_smapi (for Thinkpad)
# you need to modprobe hdaps

function get_slope () {
 [ ! -r /sys/devices/platform/hdaps/position ] && return 0
 slope=$(tr -d "()" < /sys/devices/platform/hdaps/position | cut -d, -f1)
 [ $slope -lt 400 ] && return 255
 [ $slope -gt 600 ] && return 1
 return 0
}

state[255]=right
state[0]=normal
state[1]=left

current=`xrandr | grep "^Current rotation" | awk '{ print $4 }'`

while true; do
	get_slope
	new=${state[$?]}
	[ "$new" != "$current" ] && xrandr -o $new && current=$new
	sleep 1
done
